<template>
  <div class="permission-container">
    <head-tool>
      <template slot="after">
        <el-button type="primary" @click="showAdd('0', 1)">添加权限</el-button>
      </template>
    </head-tool>

    <!-- 下面的表格 -->
    <!-- 如果表格要显示树形数据，必须加row-key -->
    <!-- defaut-expand-all:默认展开所有子节点 -->
    <!-- tree-props:
      需要绑定一个对象，对象用来绑定子节点的属性
      hasChilderen：代表判断是否有子节点的属性
     -->
    <div class="table-box">
      <el-table
        :data="tableData"
        style="width: 100%; margin-bottom: 20px"
        row-key="id"
        border
        default-expand-all
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <el-table-column prop="name" label="名称"></el-table-column>
        <el-table-column prop="code" label="标识"></el-table-column>
        <el-table-column prop="description" label="描述"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="{ row }">
            <el-button
              v-if="row.type == 1"
              type="text"
              size="small"
              @click="showAdd(row.id, 2)"
            >
              添加
            </el-button>
            <el-button type="text" size="small" @click="doEdit(row.id)">
              编辑
            </el-button>
            <el-button type="text" size="small" @click="doDel(row.id)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog :title="title" :visible.sync="showDialog" @close="cancelDialog">
      <el-form ref="form" :model="formData" :rules="rules" label-width="80px">
        <el-form-item prop="name" label="权限名称">
          <el-input v-model="formData.name"></el-input>
        </el-form-item>
        <el-form-item prop="code" label="权限标识">
          <el-input v-model="formData.code"></el-input>
        </el-form-item>
        <el-form-item prop="description" label="权限描述">
          <el-input v-model="formData.description"></el-input>
        </el-form-item>
        <el-form-item label="开启">
          <el-switch
            v-model="formData.enVisible"
            active-value="1"
            inactive-value="0"
          ></el-switch>
        </el-form-item>
        <el-row type="flex" justify="center">
          <el-button size="mini" type="primary" @click="save">确定</el-button>
          <el-button size="mini" @click="cancelDialog">取消</el-button>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  getPermissionList,
  addPermission,
  delPermission,
  updatePermission,
  getPermissionDetail,
} from "@/api/permission";

import { tranListToTreeData } from "@/utils";
export default {
  name: "permission",
  data() {
    return {
      tableData: [],
      formData: {},
      showDialog: false,
      rules: {
        name: [
          {
            required: true,
            message: "请输入权限名称",
            trigger: "blur",
          },
        ],
        code: [{ required: true, message: "请输入权限标识", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getPermissionList();
  },
  methods: {
    async getPermissionList() {
      let res = await getPermissionList();
      console.log(res);
      this.tableData = tranListToTreeData(res.data, "0");
    },
    showAdd(pid, type) {
      this.formData.pid = pid;
      this.formData.type = type;
      this.showDialog = true;
    },
    save() {
      this.$refs.form.validate(async (v) => {
        if (v) {
          if (this.formData.id) {
            await updatePermission(this.formData);
          } else {
            // 调用接口发请求
            await addPermission(this.formData);
          }
          // 弹出提示
          this.$message.success(this.title + "成功！");

          // 刷新列表
          this.getPermissionList();
          // 隐藏对话框
          this.showDialog = false;
        }
      });
    },
    async doEdit(id) {
      let res = await getPermissionDetail(id);

      this.formData = res.data;

      this.showDialog = true;
    },
    async doDel(id) {
      try {
        await this.$confirm("确定要删除该权限吗", "提示", {
          type: "warning",
        });

        await delPermission(id);

        this.getPermissionList();

        this.$message.success("删除权限成功");
      } catch (error) {
        console.log(error);
      }
    },
    cancelDialog() {
      this.formData = {};
      this.$refs.form.resetFields();
      this.showDialog = false;
    },
  },
  computed: {
    title() {
      return this.formData.id ? "编辑权限" : "新增权限";
    },
  },
};
</script>

<style lang="scss">
.permission-container {
  .table-box {
    margin: 20px;
  }
}
</style>